package com.ck.chuangke.service.impl;

import com.ck.chuangke.service.CkDeviceModelService;
import com.ck.common.service.OperationLogService;
import com.ck.constant.*;
import com.ck.dao.DeviceModelMapper;
import com.ck.exception.ServiceException;
import com.ck.model.chuangke.dto.CkDeviceModelDTO;
import com.ck.model.common.DeviceModel;
import com.ck.model.common.OperationLog;
import com.ck.model.common.User;
import com.ck.model.common.vo.DeviceModelListVO;
import com.ck.util.HttpUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName CkDeviceModelServiceImpl
 * @Description 创客管理后台-机型管理service实现类
 * @Author HanQinrui
 * @Date 2018/7/6 14:56
 * @Version 1.0
 **/
@Transactional(rollbackFor = Exception.class)
@Service(value = "ckDeviceModelService")
public class CkDeviceModelServiceImpl implements CkDeviceModelService {
    @Autowired
    private DeviceModelMapper deviceModelMapper;
    @Autowired
    private OperationLogService operationLogService;

    /**
     * 获取终端型号分页列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<DeviceModelListVO> listDeviceModelByPage(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? PageConstant.DEFAULT_PAGE_NUM : pageNum;
        pageSize = pageSize == null ? PageConstant.DEFAULT_PAGE_SIZE : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        return deviceModelMapper.listDeviceModelByPage();
    }

    /**
     * 通过型号名称查找终端机型信息
     * @param modelName 型号名称
     * @return
     */
    @Override
    public DeviceModel getDeviceModelByName(String modelName) {
        return deviceModelMapper.getDeviceModelByName(modelName);
    }

    /**
     * 新增终端机型
     * @param deviceModel
     * @param request
     */
    @Override
    public void addDeviceModel(DeviceModel deviceModel, HttpServletRequest request) {
        deviceModel.setAddTime(System.currentTimeMillis());
        deviceModelMapper.insertSelective(deviceModel);
        // TODO 保存操作日志
        OperationLog operationLog = new OperationLog();
        operationLog.setOperationContent("新增终端机型：" + deviceModel.getModelName());
        operationLog.setOperationStatus(BusinessConstant.OPERATION_LONG_OPERATION_STATUS_SUCCESS);
        operationLog.setOperationTime(System.currentTimeMillis());
        User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
        operationLog.setUserName(loginUser.getUserName());
        operationLog.setRealname(loginUser.getRealname());
        operationLog.setCopmanyCode(loginUser.getCompanyCode());
        operationLog.setOperationModule(ModuleConstant.MODULE_SYSTEM);
        operationLog.setOperationIp(HttpUtils.getRemortIP(request));
        operationLogService.saveOperationLog(operationLog);
    }

    /**
     * 批量删除终端型号
     * @param deviceModelIds 终端型号Id(多个Id之间用逗号隔开 )
     * @param request
     */
    @Override
    public void deleteDeviceModelBatch(String deviceModelIds, HttpServletRequest request) {
        String splitChar = ",";
        for (String deviceModelId : deviceModelIds.split(splitChar)) {
            if (StringUtils.isNotBlank(deviceModelId)) {
                Long id = Long.valueOf(deviceModelId);
                deviceModelMapper.deleteByPrimaryKey(id);
            }
        }
        // 保存操作日志
        OperationLog operationLog = new OperationLog();
        operationLog.setOperationContent("删除终端型号ID：" + deviceModelIds);
        operationLog.setOperationStatus(BusinessConstant.OPERATION_LONG_OPERATION_STATUS_SUCCESS);
        operationLog.setOperationTime(System.currentTimeMillis());
        User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
        operationLog.setUserName(loginUser.getUserName());
        operationLog.setRealname(loginUser.getRealname());
        operationLog.setCopmanyCode(loginUser.getCompanyCode());
        operationLog.setOperationModule(ModuleConstant.MODULE_SYSTEM);
        operationLog.setOperationIp(HttpUtils.getRemortIP(request));
        operationLogService.saveOperationLog(operationLog);
    }
    
    /**
	 * 获取终端型号列表
	 *
	 * @return List<CkDeviceModelDTO>
	 */
	@Override
	public List<CkDeviceModelDTO> listDeviceModels() {
		return deviceModelMapper.listDeviceModels();
	}
	
	@Override
	public List<CkDeviceModelDTO> listDeviceModelsSelectedUpgrade(Long deviceUpgradeId) {
		return deviceModelMapper.listDeviceModelsSelectedUpgrade(deviceUpgradeId);
	}

    @Override
    public DeviceModel getDeviceModelById(Long deviceModelId) {
        return deviceModelMapper.selectByPrimaryKey(deviceModelId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateByPrimaryKeySelective(DeviceModel deviceModel, HttpServletRequest request) throws ServiceException {
        DeviceModel deviceModelById = deviceModelMapper.selectByPrimaryKey(deviceModel.getDeviceModelId());
        if (!deviceModel.getModelName().equals(deviceModelById.getModelName())) {
            // 重复性验证
            String modelName = deviceModel.getModelName();
            DeviceModel deviceModelRepeat = deviceModelMapper.getDeviceModelByName(modelName);
            if (deviceModelRepeat != null) {
                throw new ServiceException(DeviceModelConstant.MSG_DEVICE_MODEL_EXITS);
            }
        }
        deviceModelMapper.updateByPrimaryKeySelective(deviceModel);
        operationLogService.saveSuccessOperationLog("修改终端机型：" + deviceModel.getModelName(),
                ModuleConstant.MODULE_DEVICE_MODEL_MNG, request);
    }
}
